package com.sinosoft.cloud.access.abc.service;

import com.sinosoft.cloud.access.annotations.AccessService;
import com.sinosoft.cloud.access.service.AccessServiceBean;
import com.sinosoft.cloud.rest.TradeInfo;
import com.sinosoft.lis.entity.GlobalPojo;
import com.sinosoft.utility.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 李乔
 * @Date: 2017-10-23 18:10
 * @Description: 心跳交易业务处理类
 */
@AccessService(name = "abc", transCode = "1000")
public class HeartBeatService extends AccessServiceBean {

    /**
     * 日志管理
     */
    private final Log cLogger = LogFactory.getLog(this.getClass());

    @Override
    public TradeInfo service(TradeInfo msg) {
        String errorStr = "";
        String insuTime = null;

        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dfs2 = new SimpleDateFormat("HH:mm:ss");

        GlobalPojo globalpojo = new GlobalPojo();

        try {
            globalpojo = (GlobalPojo) msg.getData(GlobalPojo.class.getName());
            Date d2 = dfs2.parse(globalpojo.getTransTime());
            Date d1 = dfs.parse(globalpojo.getTransDate());
            long l = d1.getTime() + d2.getTime();
            insuTime = String.valueOf(System.currentTimeMillis() - l);
        } catch (Exception e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            return getErrorTradeInfo(errorStr);
        }

        TradeInfo tradeInfo = new TradeInfo();
        tradeInfo.addData("insuTime", insuTime);
        tradeInfo.addData(globalpojo);
        return tradeInfo;
    }
}